package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.RoleMapper;
import com.ten.mall.service.RoleService;
import com.ten.mall.vo.PrivilegeVo;
import com.ten.mall.vo.Role;
import com.ten.mall.vo.RoleExample;
import com.ten.mall.vo.RoleExample.Criteria;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper pMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByPages(Integer start, Integer size, String name) {
		Map<String,Object> map =new HashMap<String, Object>();
		PageHelper.startPage(start, size);
		RoleExample example = new RoleExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(name)){
			c.andRoleNameLike("%"+name+"%");
		}
		List<Role> list =pMapper.selectByExample(example);
		PageInfo<Role> info =new PageInfo<>(list);
		map.put("total", info.getTotal());
		map.put("list", list);
		return map;
	}

	@Override
	public int insertRole(Role add) {
		int i =pMapper.insert(add);
		return i;
	}

	@Override
	public int updateRole(Role add) {
		int i =pMapper.updateByPrimaryKeySelective(add);
		return i;
	}

	@Override
	public int deleteRole(Integer id) {
		int i =pMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public Role selectById(Integer id) {
		Role  p =pMapper.selectByPrimaryKey(id);
		return p;
	}

	@Override
	public void insertList(List<PrivilegeVo> list) {
		for(PrivilegeVo vo:list){
			pMapper.insertDefine(vo);
		}
		
	}

}
